﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Economo.UI.Views;
using Economo.UI.AccountsServiceReference;
using Economo.Services.BusinessEntities;

namespace Economo.UI.Presenters
{
    public class ManageBankAccountsPresenter : ManageEntitiesPresenter
    {
        public ManageBankAccountsPresenter(IManageBankAccountsView view) : base(view)
        {

        }
        private IManageBankAccountsView BankAccountsView
        {
            get
            {
                return (IManageBankAccountsView)View;
            }
        }
        #region Event Methods
        #endregion
        #region CRUD Methods
        protected override void UpdateEntity(int rowIndex)
        {
            AccountsServiceClient client = new AccountsServiceClient();
            try
            {
                Economo.Services.BusinessEntities.BankAccount updatedAccount = GetAccountFromGridRow(rowIndex);

                client.UpdateBankAccount(new UpdateBankAccountRequest(updatedAccount));
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                client.Close();
            }
        }
        protected override void DeleteEntity(object id)
        {
            AccountsServiceClient client = new AccountsServiceClient();
            try
            {
                BankAccountKey key = (BankAccountKey)id;
                client.DeleteBankAccountById(new DeleteBankAccountByIdRequest(key.AccountId, key.BankId));
                List<BankAccount> accounts = (List<BankAccount>)View.EntitiesList;
                BankAccount account = (from b in accounts
                                       where (b.BankId == key.BankId && b.Id == key.AccountId)
                                       select b).FirstOrDefault<BankAccount>();
                accounts.Remove(account);
                View.EntitiesList = accounts;                
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                client.Close();
            }
        }
        protected override void AddNewEntity()
        {
            AccountsServiceClient client = new AccountsServiceClient();
            try
            {
                List<BankAccount> accounts = (List<BankAccount>)View.EntitiesList;                
                BankAccount newAccount = new BankAccount();
                newAccount.Description = "(replace)";
                if (BankAccountsView.SelectedBankId.HasValue)
                    newAccount.BankId = BankAccountsView.SelectedBankId.Value;
                else
                    newAccount.BankId = client.GetBanksByDescription(new GetBanksByDescriptionRequest("")).Banks[0].Id;
                newAccount.Id = client.InsertBankAccount(new InsertBankAccountRequest(newAccount)).AccountId;
                accounts.Add(newAccount);

                View.EntitiesList = accounts;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                client.Close();
            }
        }

        #endregion
        #region Side Methods
        private Economo.Services.BusinessEntities.BankAccount GetAccountFromGridRow(Int32 rowIndex)
        {
            Economo.Services.BusinessEntities.BankAccount accountOnGridRow = new Economo.Services.BusinessEntities.BankAccount();
            BankAccountKey key = (BankAccountKey)View.GetEntityIdFromGridRow(rowIndex);
            accountOnGridRow.Id = key.AccountId;
            accountOnGridRow.BankId = key.BankId;            
            accountOnGridRow.Description = View.GetStringColumnFromGridRow(rowIndex,"Description");
            accountOnGridRow.CurrencyId = View.GetCellValueFromGridRow<byte>(rowIndex, "CurrencyId");
             
            return accountOnGridRow;
        }
        #endregion

        #region FillMethods

        public override void FillData()
        {
            AccountsServiceClient client = new AccountsServiceClient();
            try
            {
                List<BankAccount> accounts;
                if (!BankAccountsView.SelectedBankId.HasValue)
                {
                    BankAccountsView.Banks = client.GetBanksByDescription(new GetBanksByDescriptionRequest("")).Banks;
                }
                accounts = client.GetBankAccounts(new GetBankAccountsRequest(BankAccountsView.SelectedBankId.Value)).Accounts;                
                View.EntitiesList = accounts;
                
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                client.Close();
            }
        }

        #endregion
        public struct BankAccountKey
        {
            public byte BankId
            { get; set; }
            public Int16 AccountId
            { get; set; }
        }

    }
    
}
